package com.dimples.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * 实现 BeanFactoryPostProcessor 接口，然后实现其中的postProcessBeanFactory方法，可以对spring进行一些扩展
 * 执行完内部的BeanFactoryPostProcessor后，将会执行自定义的
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/1/4
 */
@Component
public class DimplesBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory;


		String[] definitionNames = beanFactory.getBeanDefinitionNames();
		for (String name : definitionNames) {
			System.out.println("BeanDefinitionName = " + name);
		}

		GenericBeanDefinition userService = (GenericBeanDefinition) beanFactory.getBeanDefinition("userService");
//		userService.setBeanClass(MenuService.class);

		AbstractBeanDefinition abd = (AbstractBeanDefinition) beanFactory.getBeanDefinition("userService");
		abd.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
	}
}
